<?php 
namespace Admin\Controller;
use Think\Controller;
class TrainTypeController extends BaseController {



public function traintypelist(){
		$traintypeModel=M('train_type');//表明,conf中定义前缀
		$traintype=$traintypeModel->order('train_type_id asc')->select(); //desc降序 asc升序
		$this->traintype=$traintype;//name属性对应的值 $this->对应值=$sport;
			
		$this->display();
	}

 Public function adddtraintype(){
        $data=$_POST;
        $file=$_FILES;   
        //没有头像
        $traintypeModel = D('train_type'); // 实例化User模型
        $createresult=$traintypeModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
                 $traintypeModel->train_type_id = trim(I('post.train_type_id'));
                $traintypeModel->train_type_name = trim(I('post.train_type_name'));
               
                 $addresult=$traintypeModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('添加大师类型成功！', '../trainType/traintypelist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($traintypeModel->getError());  
              }

    
    }


    
public function edittraintype(){
        $train_type_id=$_GET['train_type_id'];
        $traintypeModel=M('train_type');//表明,conf中定义前缀
        $traintype= $traintypeModel->where("train_type_id=$train_type_id")->select();

        //dump($user);
        $this->traintype=$traintype;//name属性对应的值 $this->对应值=$user;
        $this->display();
    }
     
     

public function updatetraintype(){

            $data=$_POST;
            $file=$_FILES;
            
            $train_type_id=$data['train_type_id'];

            $traintypeModel = M('train_type'); // 实例化sport模型
        
      // echo ($train_course_id);
      // dump($file);
      // dump($data);
        if ($file['train_picture']['size']==0) {
            //没有修改头像
            $result=$traintypeModel->where("train_type_id=$train_type_id")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/trainphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
                 //保存表单数据 包括附件数据
                $data['train_picture']= $info['train_picture']['savename']; 
                $result=$traincourseModel->where("train_type_id=$train_type_id")->save($data);
                    if ($data['oldpic']!='nopic.jpg') {
                    $oldpicPath='Public/Uploads/trainphoto/'.$data['oldpic'];
                    unlink($oldpicPath);
                        }
                    }
            }
        if($result){
             $this->success('修改成功！','../TrainType/traintypelist');
             }
            else{
                $this->error('修改失败！');
                }
    
    }

public function deltraintype(){
  $train_type_id=$_GET['train_type_id'];
  $traintypeModel = M("train_type"); // 实例化sport对象
  $traintype=$traintypeModel->where("train_type_id=$train_type_id")->select();
  $result=$traintypeModel->where("train_type_id=$train_type_id")->delete(); // 删除sportid为5的用户数据
  //dump($sport);
    if ($traintype[0]['picture']!='nopic.jpg') {
              $picturePath='Public/Uploads/trainphoto/'.$traintype[0]['picture'];
              unlink($picturePath);
            }
    if($result){          
        $this->success('删除成功');//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
      } else {
         $this->error('删除失败'); //错误页面的默认跳转页面是返回前一页，通常不需要设置
      }



    }
}
 ?>